package liguo.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.TextView;

import com.liguo.whoisthespy.R;

import liguo.application.BaseApplication;
import uk.co.chrisjenx.calligraphy.CalligraphyTypefaceSpan;
import uk.co.chrisjenx.calligraphy.TypefaceUtils;


/**
 * @author : Extends 19:09
 * @description :
 * @email : 2563892038@qq.com
 * @date : 2017/5/9
 * @change :
 * @changer :
 */

public class StrokeTextView extends TextView
{
    private TextView outlineTextView = null;
    private int strokeColor = 0xffffffff;
    private float strokeWidth = 3;

    public StrokeTextView(Context context)
    {
        super(context);

        outlineTextView = new TextView(context);
        init(context,null);
    }

    public StrokeTextView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        outlineTextView = new TextView(context, attrs);
        init(context,attrs);
    }

    public StrokeTextView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);

        outlineTextView = new TextView(context, attrs, defStyle);
        init(context,attrs);
    }

    public void init(Context context, AttributeSet attrs)
    {
        if(attrs!=null){
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.StrokeTextView);
            strokeColor = typedArray.getColor(R.styleable.StrokeTextView_strokeColor,strokeColor);
            strokeWidth = typedArray.getDimension(R.styleable.StrokeTextView_strokeWidth,strokeWidth);
            typedArray.recycle();
        }

        TextPaint paint = outlineTextView.getPaint();
        paint.setStrokeWidth(strokeWidth);// 描边宽度
        paint.setStyle(Paint.Style.STROKE);
        outlineTextView.setTextColor(strokeColor);// 描边颜色
        outlineTextView.setGravity(getGravity());
        outlineTextView.setTypeface(BaseApplication.tf);
    }

    @Override
    public void setLayoutParams (ViewGroup.LayoutParams params)
    {
        super.setLayoutParams(params);
        outlineTextView.setLayoutParams(params);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        // 设置轮廓文字
        CharSequence outlineText = outlineTextView.getText();
        if (outlineText == null || !outlineText.equals(this.getText()))
        {
            outlineTextView.setText(getText());
            postInvalidate();
        }
        outlineTextView.measure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout (boolean changed, int left, int top, int right, int bottom)
    {
        super.onLayout(changed, left, top, right, bottom);
        outlineTextView.layout(left, top, right, bottom);
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        outlineTextView.draw(canvas);
        super.onDraw(canvas);
    }
}
